<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post_Cate_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/post_cate_entity', 'Post_Cate_Entity');
    }

    public function getAllPostCate($getAll=false)
    {
        $select = "id, slug, title, order, deleted_at";
        $this->db->select($select, true);
        $this->db->from($this->table_post_cate_name);
        if(!$getAll){
            $aWhere = array('deleted_at'=>NULL);
            $this->db->where($aWhere);
        }
        $this->db->order_by('order');
        $this->db->order_by('created_at');
        $this->db->order_by('updated_at');
        $result = $this->db->get()->result('Post_Cate_Entity');
        return $result;
    }

    public function getPostCate($aWhere=array())
    {
        $select = "id, slug, title, order, deleted_at";
        $this->db->select($select, true);
        $this->db->from($this->table_post_cate_name);
        if(count($aWhere)>0){
            $this->db->where($aWhere);
        }
        $result = $this->db->get()->result('Post_Cate_Entity');
        return $result;
    }

    /**
    * For admin only
    * Add new post cate
    */
    public function insert($post_cate){
        $this->db->trans_begin();
        $this->db->insert($this->table_post_cate_name, $post_cate);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a post cate
     */
    public function update($post_cate_id, $post_cate){
        $this->db->trans_begin();
        $this->db->where('id', $post_cate_id);
        $this->db->update($this->table_post_cate_name, $post_cate);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * check a post cate exist
     */
    public function checkPostCateExist($curId, $postCate){
        $this->db->select('id', true);
        $this->db->from($this->table_post_cate_name);
        if($curId!=0){
            $this->db->where('id !=', $curId);
        }
        $this->db->where("(slug='{$postCate['slug']}' OR title='{$postCate['title']}')");
        return $this->db->count_all_results();
    }
}
?>